/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mahumLogs.backedbeans;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Kalast
 */
public class Domain {
    private String name;
    public int count;
    private ArrayList<Log> logs;

    /**
     * Constructeur
     * @param url 
     */
    public Domain(String url) {
        this.name = this.getDomain(url);
        this.count = 0;
        this.logs = new ArrayList();
    }

    /**
     * 
     * @param obj
     * @return boolean
     */
    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Domain){
            return ((Domain)obj).name.equals(this);
        }
        return false;
    }

    /**
     * @return logs (ArrayList)
     */
    public ArrayList<Log> getLogs() {
        return logs;
    }

    /**
     * @return hash
     */
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 67 * hash + Objects.hashCode(this.name);
        return hash;
    }
    
    private static boolean testIfAddressIP(String domain){
        return domain.matches("^(?:(?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
    }
    
    public static String getDomain(String url) {
        String domain = url;
        URI uri;
        try {
            uri = new URI(url);
            
            domain = uri.getHost();
            if(domain == null || Domain.testIfAddressIP(domain)){
                return domain;
            }

            String domainEXT = domain.substring(domain.lastIndexOf(".") + 1);
            try {
                domain = domain.substring(0, domain.lastIndexOf("."));
                domain = domain.substring(domain.lastIndexOf(".") + 1, domain.length()) + "." + domainEXT;
                return domain;
            } catch(java.lang.StringIndexOutOfBoundsException ex){
                System.err.println(ex.getMessage());
                return null;
            }
            
        } catch (URISyntaxException ex) {
            System.err.println(ex.getMessage());
            return null;
        }
    }

    /**
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name 
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return 
     */
    public int getCount() {
        return this.logs.size();
    }
    
    /**
     * Fonction qui permet d'incrémenter de 1 le nombre de visiteur sur le site
     * @param l (Log) 
     */
    public void visit(Log l){
        this.logs.add(l);
    }
}
